//
//  NetworkRequest.m
//  FreeTrip
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "NetworkRequest.h"
#import "abAndeInModel.h"
#import "StrategyModel.h"
#import "BookPlaceModel.h"
#import "BookJourneyModel.h"
#import "BookTopicsModel.h"
#import "LogModel.h"
#import "LogDetailModel.h"
#import "ShopModel.h"
#import "ShopDetailModel.h"
#import "BookPlaceDetailModel.h"
#import "PhotosModel.h"
#import "BookPlaceDetailTripModel.h"
#import "BookJournDetailDayModel.h"
#import "BookJournDetailNodesModel.h"
#import "BookJourneyDetailModel.h"
#import "BookTopicsDetailModel.h"
#import "BookPlaceTravelModel.h"

@interface NetworkRequest()
@property (nonatomic, copy) networkRequest networkRequest;
@property (nonatomic, copy) networkRequestDic networkRequestDic;
@end


@implementation NetworkRequest

+ (instancetype)shareNetworeRequest

{
    static NetworkRequest *net = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        net = [[NetworkRequest alloc] init];
    });
    return net;
}
                                                                                                                 

#pragma mark ---主界面请求00----
- (void)requestWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
    {
        if (data == nil)
        {
            NSLog(@"数据请求失败");
            return ;
        }
        
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        for (NSDictionary *firDic in array)
        {
            for (NSDictionary *secDic in firDic[@"destinations"])
            {
                NSString *urlString = secDic[@"image_url"];
                NSMutableArray *firModelArray = [NSMutableArray array];
                for (NSDictionary *thrDic in secDic[@"children"])
                {
                    abAndeInModel *model = [[abAndeInModel alloc] init];
                    model.exsitUrl = urlString;
                    [model setValuesForKeysWithDictionary:thrDic];
                    [firModelArray addObject:model];
                }
                [modelArray addObject:firModelArray];
            }
        }
 
        if (self.networkRequest)
        {
            self.networkRequest(modelArray);
        }
        
    }] resume];
    
    }
#pragma mark ---strategy请求00----
- (void)requestForRaiWithUrl:(NSString *)url
{
    /*
    NSMutableArray *modelArray = [NSMutableArray array];
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          
          for (NSDictionary *firstDic in array)
          {
              NSNumber * category_type = firstDic[@"category_type"];
              NSInteger category = [category_type integerValue];
              
              NSMutableArray *firArray = [NSMutableArray array];
              for (NSDictionary *secDic in firstDic[@"pages"])
              {
                  StrategyModel *model = [[StrategyModel alloc] init];
                  model.category_type = category;
                  [model setValuesForKeysWithDictionary:secDic];
                  
                  [firArray addObject:model];
                  
              }
              [modelArray addObject:firArray];
          }
     */
    
#pragma mark  ---先下载再解析---
     NSMutableArray *modelArray = [NSMutableArray array];
    NSURLSession *session = [NSURLSession sharedSession];
    [[session downloadTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        
        NSString *jsonPath = [cachePath stringByAppendingPathComponent:response.suggestedFilename];
        
        NSFileManager *fm = [NSFileManager defaultManager];
        [fm moveItemAtPath:location.path toPath:jsonPath error:nil];
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:jsonPath]];
         NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        for (NSDictionary *firstDic in array)
        {
            NSNumber * category_type = firstDic[@"category_type"];
            NSInteger category = [category_type integerValue];
            
            NSMutableArray *firArray = [NSMutableArray array];
            for (NSDictionary *secDic in firstDic[@"pages"])
            {
                StrategyModel *model = [[StrategyModel alloc] init];
                model.category_type = category;
                [model setValuesForKeysWithDictionary:secDic];
                
                [firArray addObject:model];
                
            }
            [modelArray addObject:firArray];
        }

        
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
          
      }] resume];
    
}

#pragma mark ---bookPlace00请求---
- (void)netRequestForBookPlaceWithUrl:(NSString *)url
{

    NSMutableArray *modelArray = [NSMutableArray array];
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in array)
          {
              BookPlaceModel *model = [[BookPlaceModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [modelArray addObject:model];
          }
          
          
          
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
          }] resume];
}
#pragma mark ---bookJourney00请求---
- (void)netRequestForBookJourneyWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in array)
          {
              BookJourneyModel *model = [[BookJourneyModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [modelArray addObject:model];
          }
          
          
          
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
      }] resume];

}
#pragma mark ---bookTopics00请求---
- (void)netRequestForBookTopicsWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in array)
          {
              BookTopicsModel *model = [[BookTopicsModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [modelArray addObject:model];
          }
          
          
          
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
      }] resume];

}

#pragma mark ---贴吧Log请求---
//http://api.lis99.com/v4/club/omnibusList/1
- (void)netRequestForLogWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSDictionary *zomfirdic = zomDic[@"data"];
          for (NSDictionary *firDic in zomfirdic[@"omnibuslist"])
          {
              LogModel *model = [[LogModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [modelArray addObject:model];
          }
          
          
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
      }] resume];

}
#pragma mark ---贴吧LogDetail请求---
//http://api.lis99.com/v2/club/replylist/951930/?page=0?page=0&topic_id=951930
- (void)netRequestForLogDetailWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSDictionary *firDic = zomDic[@"data"];
          for (NSDictionary *secDic in firDic[@"topiclist"])
          {
              LogDetailModel *model = [[LogDetailModel alloc] init];
              [model setValuesForKeysWithDictionary:secDic];
              [modelArray addObject:model];
              
          }
          
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
      }] resume];

}

#pragma mark ---shopRequest00请求---
//http://chanyouji.com/api/items.json?destination_id=71&page=1
- (void)netRequestForShopWithUrl:(NSString *)url
{
    
    NSMutableArray *modelArray = [NSMutableArray array];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
         
          for (NSDictionary *secDic in zomDic[@"items"])
          {
              ShopModel *model = [[ShopModel alloc] init];
              [model setValuesForKeysWithDictionary:secDic];
              [modelArray addObject:model];
              
          }
          
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
      }] resume];

}
#pragma mark ---shopDetailRequest 请求 ---
- (void)netRequestForShopDetailWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSString *image_url = zomDic[@"image_url"];
          NSString *name = zomDic[@"name"];
          NSString *price = zomDic[@"price"];
          NSString *provider_name = zomDic[@"provider_name"];
          for (NSDictionary *dic in zomDic[@"item_contents"])
          {
              ShopDetailModel *model = [[ShopDetailModel alloc]init];
              model.image_url = image_url;
              model.name = name;
              model.price = price;
              model.provider_name = provider_name;
              [model setValuesForKeysWithDictionary:dic];
              [modelArray addObject:model];
          }
          if (self.networkRequest)
          {
              self.networkRequest(modelArray);
          }
          
      }] resume];
    
}

#pragma mark --- BookPlaceDetail00 请求 ---
//http://chanyouji.com/api/attractions/35443.json
-(void)netRequestForBookPlaceDetailWithUrl:(NSString *)url
{
    
   
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          BookPlaceDetailModel *model = [[BookPlaceDetailModel alloc] init];
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
       
          [model setValuesForKeysWithDictionary:zomDic];
          NSArray *trip = zomDic[@"attraction_trip_tags"];
           model.tripArray = [NSMutableArray array];
          for (NSDictionary *firDic in trip)
          {
              
              NSMutableArray *array = [NSMutableArray array];
              NSString *name = firDic[@"name"];
              for (NSDictionary *secDic in firDic[@"attraction_contents"])
              {
                  BookPlaceDetailTripModel *tripModel = [[BookPlaceDetailTripModel alloc] init];
                  tripModel.notesArray = [NSMutableArray array];
                  for (NSDictionary *thrDic in secDic[@"notes"])
                  {
                      PhotosModel *noteModel = [[PhotosModel alloc] init];
                      [noteModel setValuesForKeysWithDictionary:thrDic];
                      [tripModel.notesArray addObject:noteModel];
                  }
                  [tripModel setValuesForKeysWithDictionary:secDic];
                  tripModel.name = name;
                  [array addObject:tripModel];
                  
              }
              [model.tripArray addObject:array];
              

        }

          
              

              if (self.networkRequestDic)
              {
                  self.networkRequestDic(model);
              }
  
          
      }] resume];

}
#pragma mark --- BookJounDetail00 请求 ---
//http://chanyouji.com/api/plans/1693.json
-(void)netRequestForBookjournDetailWithUrl:(NSString *)url
{
    

    BookJourneyDetailModel *model = [[BookJourneyDetailModel alloc] init];
    
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSDictionary *destination = zomDic[@"destination"];
          [model setValuesForKeysWithDictionary:destination];
          [model setValuesForKeysWithDictionary:zomDic];
          model.daysArray = [NSMutableArray array];
          for (NSDictionary *firDic in zomDic[@"plan_days"])
          {
              BookJournDetailDayModel *dayModel = [[BookJournDetailDayModel alloc] init];
              dayModel.planArray = [NSMutableArray array];
              for (NSDictionary *secDic in firDic[@"plan_nodes"])
              {
                  BookJournDetailNodesModel *model = [[BookJournDetailNodesModel alloc] init];
                  [model setValuesForKeysWithDictionary:secDic];
                  NSDictionary *name = secDic[@"destination"];
                  model.name_zh_cn = name[@"name_zh_cn"];
                  [dayModel.planArray addObject:model];
              }
              [dayModel setValuesForKeysWithDictionary:firDic];
              [model.daysArray addObject:dayModel];
          }
          
          
          if (self.networkRequestDic)
          {
              self.networkRequestDic(model);
          }
          
          
      }] resume];
    

}

#pragma mark --- BookTopicsDetail00 请求 ---
-(void)netRequestForBookTopicsDetailWithUrl:(NSString *)url
{
    NSMutableArray *modelArray = [NSMutableArray array];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          NSString *kname = zomDic[@"name"];
          NSString *ktitle = zomDic[@"title"];
          NSString *Kimag = zomDic[@"image_url"];
          for (NSDictionary *firDic in zomDic[@"article_sections"])
          {
              BookTopicsDetailModel *model = [[BookTopicsDetailModel alloc] init];
              NSDictionary *dic = firDic[@"attraction"];
              NSNumber *num = dic[@"id"];
              NSInteger Id = [num integerValue];
              model.Id = Id;
              model.name = dic[@"name"];
              model.wName = kname;
              model.wTitle = ktitle;
              model.wImageurl = Kimag;
              [model setValuesForKeysWithDictionary:firDic];
              [modelArray addObject:model];
          }
                  if (self.networkRequest)
          {
              self.networkRequestDic(modelArray);
          }
          
      }] resume];

}

#pragma mark --- BookPlaceDetailTravel00 请求 ---
//http://chanyouji.com/api/attractions/35443.json?attraction_trips=true&page=1
-(void)netRequestForBookPlaceDetailTravelWithUrl:(NSString *)url
{
   
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
           NSMutableArray *modelArray = [NSMutableArray array];
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in zomDic[@"attraction_trips"])
          {
              BookPlaceTravelModel *model = [[BookPlaceTravelModel alloc] init];
              model.nodesArray = [NSMutableArray array];
              [model setValuesForKeysWithDictionary:firDic];
              for (NSDictionary *secDic in firDic[@"notes"])
              {
                  PhotosModel *notModel = [[PhotosModel alloc] init];
                  [notModel setValuesForKeysWithDictionary:secDic];
                  [model.nodesArray addObject:notModel];
              }
              
              [modelArray addObject:model];
          }
          if (self.networkRequest)
          {
              self.networkRequestDic(modelArray);
          }
          
      }] resume];
    

}


#pragma mark ---主界面请求----
- (void)netRequestWithurl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    [self requestWithUrl:url];
    self.networkRequest = networkRequest;
}

#pragma mark ---strategy请求----
- (void)netRequestForRaiWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    [self requestForRaiWithUrl:url];
    self.networkRequest = networkRequest;
}

#pragma mark ---bookPlace请求---
- (void)netRequestForBookPlaceWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    [self netRequestForBookPlaceWithUrl:url];
    self.networkRequest = networkRequest;
    
}

#pragma mark ---bookJourney请求---
- (void)netRequestForBookJourneyWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    [self netRequestForBookJourneyWithUrl:url];
     self.networkRequest = networkRequest;
}
#pragma mark ---bookTopics请求---
- (void)netRequestForBookTopicsWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    [self netRequestForBookTopicsWithUrl:url];
    self.networkRequest = networkRequest;
    
}
#pragma mark ---贴吧Log请求---
- (void)netRequestForLogWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    self.networkRequest = networkRequest;
    [self netRequestForLogWithUrl:url];
}

#pragma mark ---贴吧LogDetail请求---
- (void)netRequestForLogDetailWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    self.networkRequest = networkRequest;
    [self netRequestForLogDetailWithUrl:url];
}

#pragma mark ---shopRequest请求---
- (void)netRequestForShopWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    self.networkRequest = networkRequest;
    [self netRequestForShopWithUrl:url];
}
#pragma mark --- shopDetail 请求 ---
-(void)netRequestForShopDetailWithUrl:(NSString *)url networkRequest:(networkRequest)networkRequest
{
    self.networkRequest = networkRequest;
    [self netRequestForShopDetailWithUrl:url];
}

#pragma mark --- BookPlaceDetail 请求 ---
-(void)netRequestForBookPlaceDetailWithUrl:(NSString *)url networkRequest:(networkRequestDic)networkRequest
{
    self.networkRequestDic = networkRequest;
    [self netRequestForBookPlaceDetailWithUrl:url];
}

#pragma mark --- BookJounDetail 请求 ---
-(void)netRequestForBookjournDetailWithUrl:(NSString *)url networkRequest:(networkRequestDic)networkRequest
{
    self.networkRequestDic = networkRequest;
    [self netRequestForBookjournDetailWithUrl:url];
}
#pragma mark --- BookTopicsDetail 请求 ---
-(void)netRequestForBookTopicsDetailWithUrl:(NSString *)url networkRequest:(networkRequestDic)networkRequest;

{
    self.networkRequestDic = networkRequest;
    [self netRequestForBookTopicsDetailWithUrl:url];
}

#pragma mark --- BookPlaceDetailTravel 请求 ---
-(void)netRequestForBookPlaceDetailTravelWithUrl:(NSString *)url networkRequest:(networkRequestDic)networkRequest
{
    self.networkRequestDic = networkRequest;
    [self netRequestForBookPlaceDetailTravelWithUrl:url];
}
@end
